1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.lang3.mutable;
19
20 import java.io.Serializable;
21
22 import org.apache.commons.lang3.BooleanUtils;
23
24 /**
25 * A mutable <code>boolean</code> wrapper.
26 * <p>
27 * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter.
28 *
29 * @see Boolean
30 * @since 2.2
31 * @version $Id$
32 */
33 public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
34
35 /**
36 * Required for serialization support.
37 *
38 * @see java.io.Serializable
39 */
40 private static final long serialVersionUID = -4830728138360036487L;
41
42 /** The mutable value. */
43 private boolean value;
44
45 /**
46 * Constructs a new MutableBoolean with the default value of false.
47 */
48 public MutableBoolean() {
49 super();
50 }
51
52 /**
53 * Constructs a new MutableBoolean with the specified value.
54 *
55 * @param value the initial value to store
56 */
57 public MutableBoolean(final boolean value) {
58 super();
59 this.value = value;
60 }
61
62 /**
63 * Constructs a new MutableBoolean with the specified value.
64 *
65 * @param value the initial value to store, not null
66 * @throws NullPointerException if the object is null
67 */
68 public MutableBoolean(final Boolean value) {
69 super();
70 this.value = value.booleanValue();
71 }
72
73 //-----------------------------------------------------------------------
74 /**
75 * Gets the value as a Boolean instance.
76 *
77 * @return the value as a Boolean, never null
78 */
79 @Override
80 public Boolean getValue() {
81 return Boolean.valueOf(this.value);
82 }
83
84 /**
85 * Sets the value.
86 *
87 * @param value the value to set
88 */
89 public void setValue(final boolean value) {
90 this.value = value;
91 }
92
93 /**
94 * Sets the value to true.
95 *
96 * @since 3.3
97 */
98 public void setFalse() {
99 this.value = false;
100 }
101
102 /**
103 * Sets the value to false.
104 *
105 * @since 3.3
106 */
107 public void setTrue() {
108 this.value = true;
109 }
110
111 /**
112 * Sets the value from any Boolean instance.
113 *
114 * @param value the value to set, not null
115 * @throws NullPointerException if the object is null
116 */
117 @Override
118 public void setValue(final Boolean value) {
119 this.value = value.booleanValue();
120 }
121
122 //-----------------------------------------------------------------------
123 /**
124 * Checks if the current value is <code>true</code>.
125 *
126 * @return <code>true</code> if the current value is <code>true</code>
127 * @since 2.5
128 */
129 public boolean isTrue() {
130 return value == true;
131 }
132
133 /**
134 * Checks if the current value is <code>false</code>.
135 *
136 * @return <code>true</code> if the current value is <code>false</code>
137 * @since 2.5
138 */
139 public boolean isFalse() {
140 return value == false;
141 }
142
143 //-----------------------------------------------------------------------
144 /**
145 * Returns the value of this MutableBoolean as a boolean.
146 *
147 * @return the boolean value represented by this object.
148 */
149 public boolean booleanValue() {
150 return value;
151 }
152
153 //-----------------------------------------------------------------------
154 /**
155 * Gets this mutable as an instance of Boolean.
156 *
157 * @return a Boolean instance containing the value from this mutable, never null
158 * @since 2.5
159 */
160 public Boolean toBoolean() {
161 return Boolean.valueOf(booleanValue());
162 }
163
164 //-----------------------------------------------------------------------
165 /**
166 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
167 * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
168 * <code>boolean</code> value as this object.
169 *
170 * @param obj the object to compare with, null returns false
171 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
172 */
173 @Override
174 public boolean equals(final Object obj) {
175 if (obj instanceof MutableBoolean) {
176 return value == ((MutableBoolean) obj).booleanValue();
177 }
178 return false;
179 }
180
181 /**
182 * Returns a suitable hash code for this mutable.
183 *
184 * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
185 */
186 @Override
187 public int hashCode() {
188 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
189 }
190
191 //-----------------------------------------------------------------------
192 /**
193 * Compares this mutable to another in ascending order.
194 *
195 * @param other the other mutable to compare to, not null
196 * @return negative if this is less, zero if equal, positive if greater
197 * where false is less than true
198 */
199 @Override
200 public int compareTo(final MutableBoolean other) {
201 return BooleanUtils.compare(this.value, other.value);
202 }
203
204 //-----------------------------------------------------------------------
205 /**
206 * Returns the String value of this mutable.
207 *
208 * @return the mutable value as a string
209 */
210 @Override
211 public String toString() {
212 return String.valueOf(value);
213 }
214
215 }